import { createRouter, createWebHashHistory,type Router,type RouteRecordRaw } from 'vue-router'
import Home from '@/pages/index/views/Home.vue'
import { ElMessageBox } from 'element-plus'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router: Router = createRouter({
  history: createWebHashHistory(),
  routes
})
/**
 *  挂载路由导航守卫
 to 将要访问的路径
 from 代表从哪个路径跳转而来
 next 是个函数，表示放行 next() 放行  next('/login') 强制跳转
 */
router.beforeEach((to, from, next) => {
  // if (to.path.startsWith(process.env.publicPath + 'index.html')) {
  //     window.sessionStorage.removeItem('Token')
  //     next()
  // } else {
  const user = window.sessionStorage.getItem('Token')
  if (!user) {
    // next({
    //     path: '/Login/Login'
    // })
    ElMessageBox.alert('登录过期请前往登录').finally(() => {
      location.href = process.env.publicPath + 'login/'
    })


    // location.href = process.env.publicPath + 'login.html'
  } else {
    next()
  }
  // }
});


export default router
